<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Backend_Wallpapers extends Controller_Backend {
	
	public function action_index()
	{
		$view = new View('backend/wallpapers.index');
		
		$columns = ORM::factory('Wallpaper')->table_columns();
		$view->columns = $columns;
		
		$wallpapers = ORM::factory('Wallpaper')->order_by('sort', 'asc')->find_all();
		$view->wallpapers = $wallpapers;
		
		$info = ORM::factory('Info')->where('unique', '=', 'wallpapers')->find();
		$view->info = $info;
		
		if (isset($_POST['wallpapers']))
		{
			$info = ORM::factory('Info')->where('unique', '=', 'wallpapers')->find();
			$info->unique = 'wallpapers';
			$info->values($_POST['wallpapers'])->save();
			Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'wallpapers')));
		}
		
		$this->template->content = $view;
	}
	
	public function action_edit()
	{
		$view = new View('backend/wallpapers.edit');
		
		$id = Request::current()->param('par1');
		
		$columns = ORM::factory('Wallpaper')->table_columns();
		$view->columns = $columns;
		
		$wallpaper = ORM::factory('Wallpaper')->where('id', '=', $id)->find();
		$view->wallpaper = $wallpaper;
		
		if (isset($_POST['wallpaper']))
		{
			$wallpaper->values($_POST['wallpaper'])->save();
			$this->_upload_wallpaper_files($wallpaper);
			
			if (isset($_POST['save_and_return']))
				Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'wallpapers')));
			else
				Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'wallpapers', 'action' => 'edit', 'par1' => $wallpaper->id)));
		}
		
		$this->template->content = $view;
	}
	
	public function action_delete()
	{
		$id = Request::current()->param('par1');
		ORM::factory('Wallpaper')->where('id', '=', $id)->find()->delete();
		Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'wallpapers')));
	}
	
	protected function _upload_wallpaper_files($wallpaper)
	{
		if (isset($_FILES['wallpaper']))
		{
			if ( ! is_dir(DOCROOT.'uploads/wallpapers'))
			{
				mkdir(DOCROOT.'uploads/wallpapers');
				chmod(DOCROOT.'uploads/wallpapers', 0777);
			}
			
			foreach ($_FILES['wallpaper'] as $key => $data)
			{
				foreach ($data as $column => $value)
				{
					if ( ! empty($_FILES['wallpaper']['tmp_name'][$column]))
					{
						$filename = $_FILES['wallpaper']['name'][$column];
						$filename = URL::title(UTF8::substr($filename, 0, UTF8::strrpos('.', $filename)), '-', TRUE) .'.'. UTF8::substr($filename, UTF8::strrpos('.', $filename));
						$filename = '/wallpapers/'. UTF8::substr($filename, 1);
						
						if (file_exists(DOCROOT.'uploads'.$filename)) unlink(DOCROOT.'uploads'.$filename);
						copy($_FILES['wallpaper']['tmp_name'][$column], DOCROOT.'uploads'.$filename);
						
						$wallpaper->{$column} = $filename;
						$wallpaper->save();
					}
				}
			}
		}
	}
	
}